//
//  OrderConfirmationViewController.m
//  OMS
//
//  Created by goldeney on 14-10-10.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "OMOrderConfirmationViewController.h"
#import "OMOrderSubmitSuccessViewController.h"
#import "OMOrderSubmitErrorViewController.h"
#import "AutoTextLableView.h"

#import "NSKeyedArchiverUserLogin.h"
#import "NSDictionary+DataFormat.h"
#import "WebServiceCommonRequest.h"

@interface OMOrderConfirmationViewController ()

@property (nonatomic,strong) NSDictionary *orderData;

@end

@implementation OMOrderConfirmationViewController
@synthesize scrollView = _scrollView;

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setTitle: @"订单信息确认"];
        
        //设置发送按钮
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(submitOrderEvent)];
        self.navigationItem.rightBarButtonItem = leftButton;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)getPassingParameter:(id)object
{
    NSDictionary *dic = object;
    self.orderData = dic;
    
    NSDictionary *HTBHDic = [dic objectFormatForKey:@"合同编号"];
    NSString *HTBH = [HTBHDic objectFormatStringForKey:@"合同编号"];
    
    NSDictionary *GCMCDic = [dic objectFormatForKey:@"合同工程"];
    NSString *GCMC = [GCMCDic objectFormatStringForKey:@"工程名称"];
    
    NSDictionary *QDDJDic = [dic objectFormatForKey:@"强度等级"];
    NSString *QDDJ = [QDDJDic objectFormatStringForKey:@"StrengthLevel"];
    
    NSDictionary *JZFSDic = [dic objectFormatForKey:@"浇筑方式"];
    NSString *JZFS = [JZFSDic objectFormatStringForKey:@"Definition"];
    
    NSDictionary *FFZDDic = [dic objectFormatForKey:@"初始分发站点"];
    NSString *FFZD = [FFZDDic objectFormatStringForKey:@"U_Name"];
    
    NSMutableArray *contract = [NSMutableArray array];
    
    
    [self addContractInfoKey:@"" toKey:@"合同编号" defaults:HTBH format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"" toKey:@"工程名称" defaults:GCMC format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"施工单位" toKey:@"施工单位" defaults:@"无" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"工地地址" toKey:@"工地地址" defaults:@"未填写" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"原材要求" toKey:@"原材要求" defaults:@"未填写" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"开盘时间" toKey:@"开盘时间" defaults:@"未填写" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"" toKey:@"" defaults:@"" format:nil form:dic toArray:contract];
    
    
    [self addContractInfoKey:@"施工部位" toKey:@"施工部位" defaults:@"未填写" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"计划方量" toKey:@"计划方量" defaults:@"0立方" format:@"%@立方" form:dic toArray:contract];
    [self addContractInfoKey:@"砼类型" toKey:@"砼类型" defaults:@"普通P" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"" toKey:@"强度等级" defaults:QDDJ format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"坍落度" toKey:@"坍  落  度" defaults:@"未填写" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"" toKey:@"浇筑方式" defaults:JZFS format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"运输距离" toKey:@"运输距离" defaults:@"未填写" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"抗渗" toKey:@"抗渗" defaults:@"未填写" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"膨胀剂" toKey:@"膨胀剂" defaults:@"未填写" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"抗折" toKey:@"抗折" defaults:@"未填写" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"其他" toKey:@"其他" defaults:@"未填写" format:nil form:dic toArray:contract];
    
    NSString *xs = [dic[@"细石"]isEqualToString:@"1"]?@"细石":@"";
    NSString *zq = [dic[@"早强"]isEqualToString:@"1"]?@"早强":@"";
    NSString *lm = [dic[@"路面"]isEqualToString:@"1"]?@"路面":@"";
    NSString *zms = [dic[@"自密实"]isEqualToString:@"1"]?@"自密实":@"";
    NSString *tsxq = [NSString stringWithFormat:@"%@ %@ %@ %@",xs,zq,lm,zms];
    [self addContractInfoKey:@"" toKey:@"特殊要求" defaults:tsxq format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"" toKey:@"初始分发站点" defaults:FFZD format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"" toKey:@"" defaults:@"" format:nil form:dic toArray:contract];
    
    [self addContractInfoKey:@"工地联系人" toKey:@"工地联系人" defaults:@"无" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"工地联系电话" toKey:@"工地联系电话" defaults:@"无" format:nil form:dic toArray:contract];
    [self addContractInfoKey:@"备注" toKey:@"备        注" defaults:@"无" format:nil form:dic toArray:contract];
    
    
    AutoTextLableView *view = [[AutoTextLableView alloc] initWithFrame:CGRectMake(10, 68, 300, 0)];
    [view setDataArray:contract];
    [self.scrollView addSubview:view];
}

- (void)addContractInfoKey:(id)key toKey:(id)tokey defaults:(id)def format:(id)formatText
                      form:(NSDictionary*)dic toArray:(NSMutableArray*)array
{
    id value = [dic objectFormatForKey:key];
    if (!def) def = @"";
    
    if (!value){
        value = def;
    } else if (formatText) {
        value = [NSString stringWithFormat:formatText,value];
    }
    
    NSDictionary *valueDic = [NSDictionary dictionaryWithObject:value forKey:tokey];
    [array addObject:valueDic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)submitOrderEvent
{
    UserLoginNSCoding *userInfo = [NSKeyedArchiverUserLogin getUserLoginData];
    
    NSInteger orderID = [self.orderData objectFormatIntegerForKey:@"订单ID"];
    NSDictionary *HTBHDic = [self.orderData objectFormatForKey:@"合同编号"];
    NSString *HTBH = [HTBHDic objectFormatStringForKey:@"合同ID"];
    
    NSDictionary *GCMCDic = [self.orderData objectFormatForKey:@"合同工程"];
    NSString *GCMC = [GCMCDic objectFormatStringForKey:@"ID"];
    
    NSDictionary *QDDJDic = [self.orderData objectFormatForKey:@"强度等级"];
    NSString *QDDJ = [QDDJDic objectFormatStringForKey:@"ID"];
    
    NSDictionary *JZFSDic = [self.orderData objectFormatForKey:@"浇筑方式"];
    NSString *JZFS = [JZFSDic objectFormatStringForKey:@"Status"];
    
    NSDictionary *FFZDDic = [self.orderData objectFormatForKey:@"分发站点"];
    NSString *FFZD = [FFZDDic objectFormatStringForKey:@"Code"];
    
    NSString *GDDZ = [self.orderData objectFormatForKey:@"工地地址"];
    NSString *YCLYQ = [self.orderData objectFormatForKey:@"原材要求"];
    NSString *KPSJ = [self.orderData objectFormatForKey:@"开盘时间"];
    NSString *SGBW = [self.orderData objectFormatForKey:@"施工部位"];
    NSString *JHFL = [self.orderData objectFormatForKey:@"计划方量"];
    NSString *TLD = [self.orderData objectFormatForKey:@"坍落度"];
    NSString *YSJL = [self.orderData objectFormatForKey:@"运输距离"];
    NSString *TLX = [self.orderData objectFormatForKey:@"砼类型"];
    NSString *ks = [self.orderData objectFormatForKey:@"抗渗"];
    NSString *KS = [ks substringFromIndex:1];
    NSString *pzj = [self.orderData objectFormatForKey:@"膨胀剂"];
    NSString *PZJ = [pzj substringToIndex:pzj.length-1];
    NSString *kz = [self.orderData objectFormatForKey:@"抗折"];
    NSString *KZ = [kz substringFromIndex:1];
    NSString *XS = [self.orderData objectFormatForKey:@"细石"];
    NSString *ZQ = [self.orderData objectFormatForKey:@"早强"];
    NSString *LM = [self.orderData objectFormatForKey:@"路面"];
    NSString *ZMS = [self.orderData objectFormatForKey:@"自密实"];
    NSString *QT = [self.orderData objectFormatForKey:@"其他"];
    
    NSString *GDLXR = [self.orderData objectFormatForKey:@"工地联系人"];
    NSString *GDLXDH = [self.orderData objectFormatForKey:@"工地联系电话"];
    NSString *BZ = [self.orderData objectFormatForKey:@"备注"];
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setShowLoadingPrompt:self.navigationController labelText:@"提交订单中"];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"DDGL_DD_AU"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content addParameterKey:@"合同ID" value:HTBH];
    [content addParameterKey:@"订单ID" value:@(orderID)];
    [content addParameterKey:@"工程ID" value:GCMC];
    [content addParameterKey:@"工地地址" value:GDDZ];
    [content addParameterKey:@"原材料要求" value:YCLYQ];
    [content addParameterKey:@"计划开盘时间" value:KPSJ];
    [content addParameterKey:@"初始分发站点" value:FFZD];
    [content addParameterKey:@"施工部位" value:SGBW];
    [content addParameterKey:@"计划方量" value:JHFL];
    [content addParameterKey:@"砼强度ID" value:QDDJ];
    [content addParameterKey:@"坍落度" value:TLD];
    [content addParameterKey:@"运输距离" value:YSJL];
    [content addParameterKey:@"浇筑方式ID" value:JZFS];
    [content addParameterKey:@"制表人" value:userInfo.Id];
    [content addParameterKey:@"工地联系人" value:GDLXR];
    [content addParameterKey:@"工地联系电话" value:GDLXDH];
    [content addParameterKey:@"备注" value:BZ];
    
    [content addParameterKey:@"砼类型" value:TLX];
    [content addParameterKey:@"抗渗" value:KS];
    [content addParameterKey:@"膨胀剂" value:PZJ];
    [content addParameterKey:@"抗折" value:KZ];
    [content addParameterKey:@"细石" value:XS];
    [content addParameterKey:@"早强" value:ZQ];
    [content addParameterKey:@"路面" value:LM];
    [content addParameterKey:@"自密实" value:ZMS];
    [content addParameterKey:@"材料要求" value:QT];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0){
            firstArray = [result.resultArray firstObject];
        }
        //数据处理及显示
        [self verificationResultHandle:firstArray];
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self verificationResultHandle:nil];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self verificationResultHandle:nil];
    }];
    
}

- (void)verificationResultHandle:(NSArray*)results
{
    NSInteger orderID = [self.orderData objectFormatIntegerForKey:@"订单ID"];
    if (results && results.count>0) {
        NSDictionary *lastDictionary = [results firstObject];
        //提交成功
        if(orderID>0){
            [self showSuccessHUDText:@"修改成功"];
            MainAppDelegate *mDelegate = (MainAppDelegate*)[[UIApplication sharedApplication] delegate];
            NSArray *viewControllers = mDelegate.navigationController.viewControllers;
            AbstractBaseViewController *controller = [viewControllers objectAtIndex:viewControllers.count - 3];
            [controller backResultDataKey:@"ModifyOrder" dataValue:self.orderData];
            [mDelegate.navigationController popToViewController:controller animated:YES];
            
        } else {
            [self showSuccessHUDText:@"提交成功"];
            OMOrderSubmitSuccessViewController *mViewController = [[OMOrderSubmitSuccessViewController alloc]
                                                                   initWithValue:lastDictionary];
            [self.navigationController pushViewController:mViewController animated:YES];
        }
        
    } else {
        //网络不畅
        if(orderID>0){
            [self showErrorHUDText:@"修改失败"];
        } else {
            [self showErrorHUDText:@"提交失败"];
            OMOrderSubmitErrorViewController *mViewController = [[OMOrderSubmitErrorViewController alloc] init];
            mViewController.key = @"OrderSubmitErrorViewController";
            mViewController.backResultDelagate = self;
            [self.navigationController pushViewController:mViewController animated:YES];
        }
    }
}

#pragma mark -
#pragma mark BackResultDataDelagate

- (void) backResultDataKey:(id)key dataValue:(id)backValue
{
    if ([key isEqualToString:@"OrderSubmitErrorViewController"]) {
        //重新提交订单
        [self submitOrderEvent];
    }
}

@end
